<?php
// 开启会话
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// 数据库连接
require_once 'common/config.php';

// 检查用户是否已登录
function checkLogin() {
    if (!isset($_SESSION['user_id']) || !isset($_SESSION['username'])) {
        header('Location: index.php');
        exit();
    }
}

// 检查用户权限
function requireRole($conn, $required_role) {
    if (!isset($_SESSION['user_id'])) {
        header('Location: index.php');
        exit();
    }

    $user_id = $_SESSION['user_id'];

    // 系统管理员拥有所有权限
    $admin_sql = "SELECT COUNT(*) as count FROM user_roles WHERE user_id = ? AND role = '系统管理员'";
    $admin_stmt = $conn->prepare($admin_sql);
    $admin_stmt->bind_param('s', $user_id);
    $admin_stmt->execute();
    $admin_result = $admin_stmt->get_result();
    $admin_row = $admin_result->fetch_assoc();

    if ($admin_row['count'] > 0) {
        return; // 系统管理员通过验证
    }

    // 检查特定权限
    $role_sql = "SELECT COUNT(*) as count FROM user_roles WHERE user_id = ? AND role = ?";
    $role_stmt = $conn->prepare($role_sql);
    $role_stmt->bind_param('ss', $user_id, $required_role);
    $role_stmt->execute();
    $role_result = $role_stmt->get_result();
    $role_row = $role_result->fetch_assoc();

    if ($role_row['count'] == 0) {
        header('Location: access_denied.php');
        exit();
    }
}

$username = $_SESSION['username'] ?? '未知用户';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title ?? '考务管理系统'; ?></title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="layout-container">
        <!-- 头部区域 -->
        <header class="header">
            <div class="header-content">
                <a href="dashboard.php" class="logo">
                    <span class="logo-icon">🎓</span>
                    <span class="logo-text">考务管理系统</span>
                </a>
                <div class="user-info">
                    <div class="user-profile">
                        <span class="user-avatar">👤</span>
                        <div class="user-details">
                            <span class="user-name"><?php echo htmlspecialchars($username); ?></span>
                            <span class="user-role">
                                <?php
                                if (isset($conn) && isset($_SESSION['user_id'])) {
                                    $user_id = $_SESSION['user_id'];
                                    $role_sql = "SELECT role FROM user_roles WHERE user_id = ? ORDER BY
                                                CASE role
                                                    WHEN '系统管理员' THEN 1
                                                    WHEN '考试项目管理员' THEN 2
                                                    WHEN '人事管理员' THEN 3
                                                    ELSE 4
                                                END LIMIT 1";
                                    $role_stmt = $conn->prepare($role_sql);
                                    $role_stmt->bind_param('s', $user_id);
                                    $role_stmt->execute();
                                    $role_result = $role_stmt->get_result();
                                    if ($role_row = $role_result->fetch_assoc()) {
                                        echo htmlspecialchars($role_row['role']);
                                    } else {
                                        echo '普通用户';
                                    }
                                }
                                ?>
                            </span>
                        </div>
                    </div>
                    <a href="logout.php" class="logout-btn" onclick="return confirm('确定要退出登录吗？')">
                        <span class="logout-icon">🚪</span>
                        <span class="logout-text">退出登录</span>
                    </a>
                </div>
            </div>
        </header>

        <?php
        // 引入导航栏组件（除了index.php页面）
        $current_file = basename($_SERVER['PHP_SELF']);
        if ($current_file !== 'index.php' && isset($conn)) {
            require 'common/navigation.php';
        }
        ?>

        <!-- 主要内容区域开始 -->
        <main class="main-content">

<style>
/* 头部样式优化 */
.header {
    background: linear-gradient(135deg, #1677ff 0%, #0958d9 100%);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 1001;
}

.header-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 64px;
}

.logo {
    display: flex;
    align-items: center;
    text-decoration: none;
    color: white;
    font-weight: 600;
    font-size: 18px;
    transition: all 0.3s ease;
}

.logo:hover {
    transform: scale(1.05);
}

.logo-icon {
    font-size: 24px;
    margin-right: 10px;
}

.logo-text {
    font-size: 18px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 20px;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    color: white;
}

.user-avatar {
    width: 36px;
    height: 36px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}

.user-details {
    display: flex;
    flex-direction: column;
}

.user-name {
    font-weight: 600;
    font-size: 14px;
}

.user-role {
    font-size: 12px;
    opacity: 0.8;
}

.logout-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    text-decoration: none;
    border-radius: 6px;
    transition: all 0.3s ease;
    font-size: 14px;
}

.logout-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: translateY(-1px);
}

.logout-icon {
    font-size: 14px;
}

.logout-text {
    font-size: 13px;
}

/* 主内容区域 */
.main-content {
    padding: 30px 20px;
    max-width: 1200px;
    margin: 0 auto;
    min-height: calc(100vh - 128px);
}

/* 响应式设计 */
@media (max-width: 768px) {
    .header-content {
        padding: 0 15px;
        height: 56px;
    }

    .logo-text {
        font-size: 16px;
    }

    .logo-icon {
        font-size: 20px;
        margin-right: 8px;
    }

    .user-info {
        gap: 15px;
    }

    .user-details {
        display: none;
    }

    .logout-text {
        display: none;
    }

    .main-content {
        padding: 20px 15px;
    }
}

@media (max-width: 480px) {
    .header-content {
        padding: 0 10px;
    }

    .user-info {
        gap: 10px;
    }

    .user-avatar {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }

    .logout-btn {
        padding: 6px 12px;
    }
}
</style>